//
//  WHYEmoticonAttachment.swift
//  emoticonKeyboard
//
//  Created by 王洪运 on 15/11/30.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYEmoticonAttachment: NSTextAttachment {

    var emoticon: WHYEmoticon
    
    init(emoticon: WHYEmoticon) {
        self.emoticon = emoticon
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func imageText(font:UIFont) -> NSAttributedString {
        
        image = UIImage(contentsOfFile: emoticon.imagePath)
        //        设置attachment的bounds
        let lineHeight = font.lineHeight
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        //        将NSTextAttachment再转成NSAttributedString
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        //        初始化字体属性
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText
    }
    
}
